package com.ttnet.beans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.ttnet.bo.FileBo;
import com.ttnet.model.Football;
import com.ttnet.model.Weather;

@ViewScoped
@ManagedBean(name = "footballBean")
public class FootballBean {

	@ManagedProperty(value = "#{fileBo}", name = "fileBo")
	private FileBo fileBo;

	private UploadedFile file;

	StringBuffer readFromDatFile;

	private List<Football> footballList;
	private List<Football> resultFootballList;

	@PostConstruct
	public void initModel() {

	}

	public void handleFileUpload(FileUploadEvent event) {

		file = event.getFile();
		// String absolutePath = new java.io.File("").getAbsolutePath();
		// System.out.println("absolutePath : " + absolutePath);
		try {
			List<String> omitStringList = new ArrayList<String>();
			omitStringList.add("---");
			List<String> ignoreStringList = new ArrayList<String>();
			ignoreStringList.add("-");
			readFromDatFile = fileBo.readFromDatFile(file.getInputstream(), "Team", "</pre>", omitStringList, ignoreStringList, -1);

		} catch (IOException e) {
			e.printStackTrace();
			file = null;
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, event.getFile().getFileName() + " is not uploaded or validated", "");
			FacesContext.getCurrentInstance().addMessage(null, msg);

		}
		footballList = new ArrayList<Football>();
		resultFootballList=null;
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getFile().getFileName() + " is uploaded and validated", "");
		FacesContext.getCurrentInstance().addMessage(null, msg);

	}

	public void show(ActionEvent event) {
		footballList = new ArrayList<Football>();
		resultFootballList = new ArrayList<Football>();
		StringTokenizer st = new StringTokenizer(readFromDatFile.toString(), "\n");
		while (st.hasMoreTokens()) {
			footballList.add(new Football(st.nextToken()));
		}
		Football[] a = new Football[footballList.size()];
		footballList.toArray(a);
		Arrays.sort(a, Football.FootballOriginalComparator);
		for (int i = 0, diff = a[0].getA() - a[0].getF(); i < a.length; i++) {
			if (diff >= 0 && diff == a[i].getA() - a[i].getF())
				resultFootballList.add(a[i]);
		}

	}

	public FileBo getFileBo() {
		return fileBo;
	}

	public void setFileBo(FileBo fileBo) {
		this.fileBo = fileBo;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public StringBuffer getReadFromDatFile() {
		return readFromDatFile;
	}

	public void setReadFromDatFile(StringBuffer readFromDatFile) {
		this.readFromDatFile = readFromDatFile;
	}

	public List<Football> getFootballList() {
		return footballList;
	}

	public void setFootballList(List<Football> footballList) {
		this.footballList = footballList;
	}

	public List<Football> getResultFootballList() {
		return resultFootballList;
	}

	public void setResultFootballList(List<Football> resultFootballList) {
		this.resultFootballList = resultFootballList;
	}

}
